﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using QuanLyBanHang.DAL;
using QuanLyBanHang_NewApp.Entity;
using QuanLyBanHang_NewApp.DAL;

namespace QuanLyBanHang_NewApp.BUS
{
    public class KhachHangBUS
    {
        KhachHangDAL khDal;

        public KhachHangBUS()
        {
            khDal = new KhachHangDAL();
        }

        public bool themKH(KhachHang kh)
        {
            string hoten = kh.getAttribute(1);
            string dt = kh.getAttribute(2);
            string dc = kh.getAttribute(3);
            string cmt = kh.getAttribute(4);
            string mathe = kh.getAttribute(5);
            string sotien = kh.getAttribute(6);

            if (!khDal.insertCard(mathe, long.Parse(sotien))) return false;
            return khDal.insertKH(hoten, dc, cmt, dt, mathe);
        }

        public bool capnhatKH(KhachHang kh)
        {
            string hoten = kh.getAttribute(1);
            string dt = kh.getAttribute(2);
            string dc = kh.getAttribute(3);
            string cmt = kh.getAttribute(4);
            string mathe = kh.getAttribute(5);

            long makh = long.Parse(kh.getAttribute(0));

            return khDal.updateKH(makh, hoten, dc, cmt, dt, mathe);
        }

        public DataTable getAllKH()
        {
            return khDal.selectKH("");
        }

        public bool xoaKH(long id, string mathe)
        {
            if (!khDal.deleteKH(id)) return false;
            return khDal.deleteCard(mathe);
        }

        public DataTable getByName(string hoten)
        {
            return khDal.selectKH(hoten);
        }

        public bool capnhatThe(long makh, long sotien)
        {
            return khDal.updateCardInfo(makh, sotien);
        }

        public KhachHang timTheoIDThe(string mathe)
        {
            DataTable dt;
            dt = khDal.selectByCardID(mathe);
            if (dt.Rows.Count == 0) return null;
            DataRow row = dt.Rows[0];
            long ma = long.Parse(row["MaKH"].ToString());
            string ten = row["TenKH"].ToString();
            string dc = row["DiaChi"].ToString();
            string sodt = row["SoDT"].ToString();
            string cmt = row["SoCMT"].ToString();
            string the = row[5].ToString().Trim();
            string tienthe = row["TienThe"].ToString();


            KhachHang kh = new KhachHang(ma, ten, sodt, dc, cmt, the, tienthe);
            return kh;
        }
        
    }
}
